package net.lcheng.service.impl;

import net.lcheng.dao.SysPermissionsMapper;
import net.lcheng.dao.SysUsersMapper;
import net.lcheng.model.*;
import net.lcheng.service.SysPermissionsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by qiliping on 15/12/16.
 */
@Service("SysPermissionsService")
public class SysPermissionsServiceImpl implements SysPermissionsService {
    @Resource
    private SysPermissionsMapper mapper;
    @Resource
    private SysUsersMapper usersMapper;

    @Override
    public int add(String name, String key,String url, int modelId,int menuId) {
        SysPermissions model = new SysPermissions();
        model.setCreateOn(new Date());
        model.setIsMenu(menuId==0?1:0);
        model.setIsModel(modelId==0?1:0);
        model.setIsShow(0);
        model.setModelId(modelId);

        model.setName(name);
        model.setParentId(menuId);
        model.setPermissionKey(key);
        model.setUrl(url);
        return mapper.insert(model);
    }

    @Override
    public int edit(int id,String name, String key,String url, int modelId,int menuId) {
        SysPermissions model = new SysPermissions();
        model.setSysPermissionId(id);
        model.setName(name);
        model.setPermissionKey(key);
        model.setIsMenu(menuId==0?1:0);
        model.setIsModel(modelId==0?1:0);
        model.setParentId(menuId);
        model.setModelId(modelId);
        model.setUrl(url);
        return mapper.updateByPrimaryKeySelective(model);
    }

    @Override
    public List<SysPermissions> getAll() {
        SysPermissionsExample example = new SysPermissionsExample();

        return mapper.selectByExample(example);
    }

    /***
     * 按层级获取所有权限点
     * @return
     */
    @Override
    public List<SysPermissionsModel> getList() {
        List<SysPermissionsModel> list = new ArrayList<>();

        return getListByParent(0);
    }

    @Override
    public SysPermissions getById(int id) {
        return mapper.selectByPrimaryKey(id);
    }



    @Override
    public List<SysPermissions> getModel() {
        SysPermissionsExample example = new SysPermissionsExample();
        example.or().andIsModelEqualTo(1);

        return mapper.selectByExample(example);
    }

    /***
     * 获取用户有权限的模块
     * @param userName
     * @return
     */
    @Override
    public List<SysPermissionsModel> getModelByUserName(String userName) {
        List<SysPermissionsModel> tempList = new ArrayList<>();

        /**查询用户信息*/
        SysUsersExample usersExample = new SysUsersExample();
        usersExample.or().andUsernameEqualTo(userName);
        List<SysUsers> users = usersMapper.selectByExample(usersExample);
        if(users!=null&&users.size()>0){
            if(users.get(0).getIsAdministrator()==0){
                /**非管理员*/
                /**模块*/
                List<SysPermissions> modelList = mapper.selectByUserName(userName,0);

                for (SysPermissions pmss:modelList) {
                    SysPermissionsModel pmssModel = new SysPermissionsModel();
                    pmssModel.setName(pmss.getName());
                    pmssModel.setPermissionKey(pmss.getPermissionKey());
                    pmssModel.setSysPermissionId(pmss.getSysPermissionId());
                    pmssModel.setCreateOn(pmss.getCreateOn());
                    pmssModel.setParentId(pmss.getParentId());

                    /**菜单*/
                    List<SysPermissions> menuList = mapper.selectByUserName(userName,pmss.getSysPermissionId());
                    pmssModel.setMenus(menuList);
                    tempList.add(pmssModel);
                }
            }else {
                /**管理员*/
                SysPermissionsExample example = new SysPermissionsExample();
                example.or().andIsModelEqualTo(1);
                example.setOrderByClause("order_num desc");
                /**模块*/
                List<SysPermissions> modelList = mapper.selectByExample(example);
                for (SysPermissions pmss:modelList) {
                    SysPermissionsModel pmssModel = new SysPermissionsModel();
                    pmssModel.setName(pmss.getName());
                    pmssModel.setPermissionKey(pmss.getPermissionKey());
                    pmssModel.setSysPermissionId(pmss.getSysPermissionId());
                    pmssModel.setCreateOn(pmss.getCreateOn());
                    pmssModel.setParentId(pmss.getParentId());

                    /**菜单*/
                    SysPermissionsExample menuExample = new SysPermissionsExample();
                    menuExample.or().andModelIdEqualTo(pmss.getSysPermissionId()).andIsMenuEqualTo(1);
                    menuExample.setOrderByClause("order_num desc");

                    List<SysPermissions> menuList = mapper.selectByExample(menuExample);
                    pmssModel.setMenus(menuList);
                    tempList.add(pmssModel);
                }
            }
        }



        return tempList;
    }

    @Override
    public List<SysPermissions> getMenuByModel(int model) {
        SysPermissionsExample example = new SysPermissionsExample();
        example.or().andModelIdEqualTo(model).andIsMenuEqualTo(1);

        return mapper.selectByExample(example);
    }


    private List<SysPermissionsModel> getListByParent(int parent){
        List<SysPermissionsModel> tempList = new ArrayList<>();

        SysPermissionsExample example = new SysPermissionsExample();
        example.or().andModelIdEqualTo(parent);

        List<SysPermissions> pmlist = mapper.selectByExample(example);

        for (SysPermissions pmss:pmlist) {
            SysPermissionsModel pmssModel = new SysPermissionsModel();
            pmssModel.setName(pmss.getName());
            pmssModel.setPermissionKey(pmss.getPermissionKey());
            pmssModel.setSysPermissionId(pmss.getSysPermissionId());
            pmssModel.setCreateOn(pmss.getCreateOn());
            pmssModel.setParentId(pmss.getParentId());
            pmssModel.setUrl(pmss.getUrl());
            pmssModel.setIsModel(pmss.getIsModel());
            pmssModel.setIsMenu(pmss.getIsMenu());
            pmssModel.setChilds(getListByParent(pmss.getSysPermissionId()));
//            list.add(pmssModel);
            tempList.add(pmssModel);
        }
        return tempList;
    }

}
